package libs.yzx.zaza;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/7/29.
 */
public class BindServiceActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        bindService(new Intent(this, MyBindService.class), new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                MyBindService.TestBinder tv = (MyBindService.TestBinder) service;
                Toast.makeText(BindServiceActivity.this, tv.get(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, BIND_AUTO_CREATE);


        new Handler().postDelayed(new Runnable() {
            public void run() {



                bindService(new Intent(BindServiceActivity.this, MyBindService.class), new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service) {
                        MyBindService.TestBinder tv = (MyBindService.TestBinder) service;
                        Toast.makeText(BindServiceActivity.this, tv.get(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onServiceDisconnected(ComponentName name) {

                    }
                }, BIND_AUTO_CREATE);

            }
        },1000);

    }

}
